﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Excel;
using log4net.Appender;
using Telerik.Web.UI;

public partial class EmailDeliveryReport : BaseReportPage
{
    public override string Icon
    {
        get
        {
            return "images/report_icon.png";
        }
    }

	private bool _isExporting;

    protected void Page_Load(object sender, EventArgs e)
    {
		if (!IsPostBack)
		{
			FilldropLstSelection();

			DayOfWeek firstDayInWeek = DayOfWeek.Sunday;
			DayOfWeek lastDayInWeek = DayOfWeek.Saturday;

			DateTime currentDate = DateTime.Today;
			while (currentDate.DayOfWeek != firstDayInWeek)
				currentDate = currentDate.AddDays(-1);

			radDP_StartDate.SelectedDate = currentDate;
			
			currentDate = DateTime.Today;

			while (currentDate.DayOfWeek != lastDayInWeek)
				currentDate = currentDate.AddDays(1);

			radDP_EndDate.SelectedDate = currentDate;

		}
    }

    private void FilldropLstSelection()
    {
        DataLayer dal = new DataLayer();
        string message;

        dropLstSection.Items.Clear();

        dropLstSection.DataSource = dal.GetSections(PAConfiguration.ConnectionString, out message);
        dropLstSection.DataValueField = "section_id";
        dropLstSection.DataTextField = "section_title";
        dropLstSection.DataBind();

        dropLstSection.Items.Insert(0, new ListItem("Select a section", "all"));
    }

    
    protected void lbExport_Click(object sender, ImageClickEventArgs e)
    {
		/*_isExporting = true;
		ExportToExcel("EmailDeliveryReport", rgEmailDeliveryReport);*/

		Fill();

		DataTable dt = rgEmailDeliveryReport.DataSource as DataTable;

		if (dt != null)
		{
			int startRow = 1;
			string[] listFields = new string[]
			                      	{
			                      		"DATE_SENT", "TOTALS_NUMBER_OF_EMAILS", "DELIVERED", "FAILED"
			                      	};
			Excel ex = Utils.GridToExcel(dt, listFields, "EmailDeliveryReport", false, ref startRow);
			Worksheet ws = ex.Worksheets[0];
			for (int i = 1; i < startRow - 1; ++i)
			{
				DataRow row = dt.Rows[i - 1];
				for (int j = 0; j < listFields.Length; ++j)
				{
					ws.Cells[i, j].Style.Borders[BorderType.LeftBorder].Color = Color.Black;
					ws.Cells[i, j].Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;

					ws.Cells[i, j].Style.Borders[BorderType.RightBorder].Color = Color.Black;
					ws.Cells[i, j].Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

					ws.Cells[i, j].Style.Borders[BorderType.BottomBorder].Color = Color.Black;
					ws.Cells[i, j].Style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

					ws.Cells[i, j].Style.HorizontalAlignment = TextAlignmentType.Center;
				}
			}



			ex.Save("EmailDeliveryReport Download.xls", FileFormatType.Excel2003, SaveType.OpenInExcel, Page.Response);
			Page.Response.End();
		}
    }

    protected void rgEmailDeliveryReport_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        Fill();
    }

    protected void btnGo_Click(object sender, EventArgs args)
    {
        Fill();
        rgEmailDeliveryReport.Rebind();
    }

	protected override void Fill()
	{
		string message;

		decimal outSectionId; // 3 6 11
		decimal? sectionId = decimal.TryParse(dropLstSection.SelectedValue, out outSectionId) ? (decimal?)outSectionId : null;

		DateTime startDate = radDP_StartDate.SelectedDate == null ? DateTime.MinValue : (DateTime)radDP_StartDate.SelectedDate;
		DateTime endDate = radDP_EndDate.SelectedDate == null ? DateTime.Now : (DateTime)radDP_EndDate.SelectedDate;

		rgEmailDeliveryReport.DataSource = new DataLayer().GetEmailDeliveryReport(PAConfiguration.ConnectionString, startDate, endDate, sectionId, out message);
	}

	protected void rgEmailDeliveryReport_ItemCreated(object sender, GridItemEventArgs e)
	{
		RadGrid grid = sender as RadGrid;

		if (e.Item is GridDataItem)
		{
			DataRowView drv = e.Item.DataItem as DataRowView;

			if (drv != null)
			{
				if (_isExporting)
				{
					foreach (TableCell c in e.Item.Cells)
					{
						c.HorizontalAlign = HorizontalAlign.Center;
						//c.ForeColor = Color.FromName("White");
					}

					e.Item.Cells[2].Text = drv["DATE_SENT"].ToString();
					e.Item.Cells[3].Text = drv["TOTALS_NUMBER_OF_EMAILS"].ToString();
					e.Item.Cells[4].Text = drv["DELIVERED"].ToString();
					e.Item.Cells[5].Text = drv["FAILED"].ToString();
				}
			}
		}
	}
}